<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1. 导入vue的脚本文件-->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="app"> 
        <!-- v-model双向改变，:value单向改变（值变页面变，页面变值不变） -->
        <input type="text" v-model="name" 
        @keyup.enter="addUser"
        placeholder="请输入用户名">
        <button @click="addUser">添加用户</button>
    

        <ul>
            <li v-for="(user, index) in userList" :key="user.id">
                <input type="checkbox" /> 姓名：{{user.name}}
            </li>
        </ul>
    </div>

    <script>
        const vm = {
            data: function() {
                return {
                    userList: [
                        {id:1,name: '张三'},
                        {id:2,name: '李四'},
                        {id:3,name: '王五'}
                    ],
                    name:'',
                    nextId: 4
                }
            },
            methods: {
                addUser() {
                    this.userList.push({id: this.nextId, name: this.name})
                    this.name= ''
                    this.nextId++
                }
            }
        }
        const app = Vue.createApp(vm)
        app.mount('#app')
    </script>
</body>
</html>